package com.dz.lm.web.controller.user;

import com.alibaba.fastjson.JSONObject;
import com.dz.lm.common.exception.ServiceException;
import com.dz.lm.common.utils.json.JsonUtils;
import com.dz.lm.service.reminder.ReminderService;
import com.dz.lm.web.controller.BasicControl;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Vector;

@RestController
@RequestMapping(value = "/rewriteUrl/reminder", produces = "application/json")
public class ReminderController extends BasicControl {

	public Logger logger = LogManager.getLogger(ReminderController.class);
	
    @Autowired
    private ReminderService reminderService;
    
    /**
     * 获取新记录提醒列表
     * 
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/qryReminderInfo")
    public Map<String, Object> qryReminderInfo(HttpServletRequest request) throws ServiceException {
		String reqData = request.getAttribute("reqData") != null ? request.getAttribute("reqData").toString() : "";
    	JSONObject _reqData = JSONObject.parseObject(reqData);
    	JSONObject subjo = new JSONObject();;
		Map<String, Object> param = JsonUtils.readValue(_reqData.toString());
		param.put("userId", request.getAttribute("userId"));
		List<Map<String, Object>> reminderList = reminderService.qryReminderInfo(param);
		subjo.put("result", "00");
    	subjo.put("message", "SUCCESS");
    	subjo.put("reminderList", reminderList);
		return parameterEncrypt(request, subjo);
    }

    /**
     * 用户点击提醒内容
     * 
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/clickReminderInfo")
    public Map<String, Object> clickReminderInfo(HttpServletRequest request) throws ServiceException {
		String reqData = request.getAttribute("reqData") != null ? request.getAttribute("reqData").toString() : "";
    	JSONObject _reqData = JSONObject.parseObject(reqData);
    	JSONObject subjo = new JSONObject();
    	//参数校验
		Vector<String> inparam = new Vector<String>();
		inparam.add("type");
		for (String in : inparam) {
			if (!_reqData.containsKey(in)) {
		    	subjo.put("result", "01");
		    	subjo.put("message", "参数提醒内容类型不能为空");
		    	return parameterEncrypt(request, subjo);
			}
		}
		Map<String, Object> param = JsonUtils.readValue(_reqData.toString());
		if (param.get("type") == null || "".equals(param.get("type"))) {
	    	subjo.put("result", "01");
	    	subjo.put("message", "参数提醒内容类型不能为空");
	    	return parameterEncrypt(request, subjo);
		}
		param.put("userId", request.getAttribute("userId"));
		reminderService.clickReminderInfo(param);
		subjo.put("result", "00");
    	subjo.put("message", "已读");
		return parameterEncrypt(request, subjo);
    }
}
